package cn.bugDance.trigger.job;


import cn.bugDance.domain.award.model.entity.TaskEntity;
import cn.bugDance.domain.task.service.ITaskService;
import cn.bugstack.middleware.db.router.strategy.IDBRouterStrategy;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.micrometer.core.annotation.Timed;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class SendUserAwardJob {

    @Resource
    private ITaskService taskService;
    @Resource
    private IDBRouterStrategy dbRouter;
    @Resource
    private ThreadPoolExecutor threadPoolExecutor;
    @Resource
    private RedissonClient redissonClient;

    @Scheduled(cron = "0/5 * * * * ?")
    public void exec1(){
        RLock lock = redissonClient.getLock("big-market-SendMessageTaskJob_DB1");
        boolean result = false;
        try{
            result = lock.tryLock(3, 3, TimeUnit.SECONDS);
            if(!result) return;
            dbRouter.setDBKey(1);
            dbRouter.setTBKey(0);

            List<TaskEntity> taskEntities = taskService.queryNoCompletedTask();
            if(null == taskEntities || taskEntities.isEmpty()) return;
            log.info("【任务触发层-SendUserAwardJob】: 查询到为未完成的发奖任务, 开始执行补发任务");
            for(TaskEntity taskEntity : taskEntities){
                try{
                    taskService.sendAward(taskEntity);
                    taskService.updateTaskStateSuccess(taskEntity);
                }catch (Exception e){
                    log.warn("【任务触发层-SendUserAwardJob】: 奖品补发任务失败, 将会在下一次定时任务再次触发");
                    taskService.updateTaskStateFail(taskEntity);
                }
            }
        }catch (Exception e){
            log.error("【发奖触发层-SendUserAwardJob】定时任务，扫描MQ任务表发送消息失败, message: {}", e.getMessage());
        } finally {
            dbRouter.clear();
        }
    }

    @Scheduled(cron = "0/5 * * * * ?")
    public void exec2(){
        RLock lock = redissonClient.getLock("big-market-SendMessageTaskJob_DB2");
        boolean result = false;
        try{
            result = lock.tryLock(3, 3, TimeUnit.SECONDS);
            if(!result) return;
            dbRouter.setDBKey(2);
            dbRouter.setTBKey(0);

            List<TaskEntity> taskEntities = taskService.queryNoCompletedTask();
            if(null == taskEntities || taskEntities.isEmpty()) return;
            log.info("【任务触发层-SendUserAwardJob】: 查询到为未完成的发奖任务, 开始执行补发任务");
            for(TaskEntity taskEntity : taskEntities){
                try{
                    taskService.sendAward(taskEntity);
                    taskService.updateTaskStateSuccess(taskEntity);
                }catch (Exception e){
                    log.warn("【任务触发层-SendUserAwardJob】: 奖品补发任务失败, 将会在下一次定时任务再次触发");
                    taskService.updateTaskStateFail(taskEntity);
                }
            }
        }catch (Exception e){
            log.error("【发奖触发层-SendUserAwardJob】定时任务，扫描MQ任务表发送消息失败, message: {}", e.getMessage());
        } finally {
            dbRouter.clear();
        }
    }
}
